MALLOC
Section: MINTLIB LIBRARY FUNCTIONS
(3)
Updated: 3 March 1993
Index
Return to Main Contents
NAME
malloc, free, realloc, calloc, alloca - memory allocator
SYNOPSIS
#include <stdlib.h>
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
void *calloc(size_t num_elems, size_t elem_size);
void *alloca(size_t size);
DESCRIPTION
These routines provide a general-purpose memory allocation
package. They maintain a table of free blocks for efficient
allocation and coalescing of free storage. When there is no
suitable space already free, the allocation routines call
Malloc to get more memory from the system.
Each of the allocation routines returns a pointer to space
suitably aligned for storage of any type of object. Each
returns a NULL pointer if the request cannot be completed or
if an area of size zero is requested.
malloc returns a pointer to a block of at least size bytes.
free releases a previously allocated block. Its argument
is a pointer to a block previously allocated by malloc,
calloc or realloc.
realloc changes the size of a block referenced to by ptr
to size bytes and returns a pointer to the (possibly moved)
block. The contents will be unchanged up to the lesser of
the new and old sizes. If unable to honor a reallocation
request, realloc leaves it first argument unaltered.
Using realloc with a block freed before is an error.
realloc(NULL, size) is the same as malloc(size).
realloc(ptr, 0) is the same as free(ptr).
calloc uses malloc to allocate space for an array of
num_elems elements of size elem_size, initialises the space
to zeros, and returns a pointer to the initialised block.
alloca allocates size bytes of space in the stack frame
of the caller, and returns a pointer to the allocated block.
This temporary space is automatically freed when the caller
returns. Note that if the allocated block is beyond the
current stack limit, the resulting behavior is undefined.
RETURN VALUES
On success, malloc, calloc, realloc and alloca
return a pointer to space suitably aligned for storage
of any type of object. On failure, they return NULL.
SEE ALSO
Malloc(2),
getrlimit(3)
WARNINGS
On UN*X machines, malloc and realloc return a non-NULL pointer
if size is 0, and calloc returns a non-NULL pointer if num_elems
or elem_size is 0. The mintlibs follow the ANSI-C standard and
return NULL in these circumstances.
alloca is machine-, compiler-, and most of all, system-
dependent. Its use is strongly discouraged.
NOTES
GNU software is uncommonly fond of alloca.
To use alloca with Pure-C in the current version of
the mintlibs, the caller must be compiled with the -S
option set or the program will crash.
Index
- NAME
-
- SYNOPSIS
-
- DESCRIPTION
-
- RETURN VALUES
-
- SEE ALSO
-
- WARNINGS
-
- NOTES
-
This document was created by
man2html,
using the manual pages.
Time: 11:14:55 GMT, June 22, 2025